CHR$(4) explained
Buffer supplied by Grant Kwai
Copyright (c) 1991 Apple Users' Group, Sydney
Republished from Applecations, a publication of the Apple Users' Group, Sydney, Australia.
Recently I posted a message on the Augur Bulletin Board System concerning a little problem I have been having with some of my programs. I knew there must be a logical explanation so I thought someone out there must know why. In the following transcript, I have the advice of many who knew my problem and were able to tell me a solution. I am sure someone out there has encountered a similar problem so I thought it may be of benefit to other Apple users. Much thanks to all those who replied to my message.
[Sub-editor's note: BBS's are a quick source of information on almost any topic. There are hundreds of callers each week. Make new friends - buy a modem!]
From: GRANT KWAI
When writing some BASIC programs, I sometimes come across a small problem which seems to crop up every now and then. Basically, when I enter something like PRINT CHR$(4);"CATALOG", you would normally expect a catalog of the disk. however, sometimes it will just print 'CATALOG' to the screen instead of executing the command. This problem has been baffling me for many years now. Has anyone encountered this problem as well? Can anyone offer any suggestions as to why this is happening? Any help would be much appreciated.
From: MICHAEL CHAN
I think it has something to do with what you have running or what's in memory beforehand. To play it safe and to follow good programming practice, do this;
D$=CHR$(13)+CHR$(4)
PRINT D$;"CATALOG"
That will make it go no matter what is in memory (well...almost).
From: MICHAEL CHAN
In addition to my reply...your PRINT CHR$(4) may not be working because you previously had a;
PRINT "ANYTHING";
Now, I can leave you to think about what this does or I can tell you. For those who can't figure it out...if you put the above line before the PRINT CHR$(4) this is what the Apple thinks;
ANYTHING<CTRL-D>CATALOG
Yech! Keep to good programming habits and assign your variables rather than printing out a couple of hundred (and writing it out a couple of hundred times); PRINT CHR$(4).
Use your String variables.
From: COLIN KHONG
Your problem with that little quirk of Applesoft may be due to the fact that you have issued a GET A$ (for example) on the line above. It seems that in Applesoft, after issuing a GET command, all subsequent disk access will not work. The simple remedy (if this is your problem) is to just issue a few print commands. i.e. (line number) print:print
That should do the trick! Colin.
From: GRANT KWAI
Thanks I'll have to give it a go. I was always wondering why it only worked sometimes.
From: FRANK BOYES
In 'BASIC Programming With ProDOS' (page 67) to use a ProDOS command from within a program, you must print a string consisting of a [ CONTROL ] - [ D ] as the first thing on a printed line also recommended prodos convention is that CHR$ (13) + CHR$ (4) SHOULD NOT BE USED, and left for those using the old Dos
From: FRANK BOYES
Further to my previous message, I have since had time to look further at 'BASIC Programming With ProDOS' and in chapter 5 (page 68) it states; Print each ProDOS command on a new line Control-D must be preceded by a [RETURN] that is
Control-D must be the first character on a printed line. Thus the following program will not work
30 D$ = CHR$ (4)
40 PRINT "AUTUMN "; (note semi-colon)
50 PRINT D$;"CATALOG"
Instead of listing the volume directory, this program prints AUTUMN CATALOG if you program is unexpectedly printing ProDOS commands on the screen - and never at the beginning of a new line - this is probably why NOTICE; If your program contains a statement like this, with [RETURN] preceding Control-D: D$ = CHR$ (13) + CHR$ (4) Your program will only work with DOS and not with ProDOS. Remember that Control-D must be the first thing on a printed line the above is probably copyright Apple Computer, as it is copied directly from the section 'debugging your programs' subsection 'things to watch out for' of course if you are not using ProDOS, you may be better to ignore the above.
From: DARREN LANGER
Depends what operating system you are using.
If you are using Prodos, I'm not sure what it could be, but under DOS3.3, if your statement does not follow a carriage return, Dos3.3 ignores the command and applesoft then gets hold of it and prints it as a normal statement. This may also happen under Prodos, although I can't remember exactly.
The solution under Dos3.3 is to make the statement as follows.
D$=CHR$(13)+CHR$(4) PRINT D$;"CATALOG"
If the problem occurs under prodos it could well be for the same reason, however the above solution will not work. The best thing to do is ensure that the statement is on a line of its own after a carriage return.

THIS CONTENT COPYRIGHT © 2007, APPLE MACINTOSH USERS' GROUP, SYDNEY
Permission has been obtained to make this material available on the Internet.

Permission is hereby granted for non-profit user groups to republish this content.
PLEASE CREDIT THE AUTHOR AND THE SOURCE: Applecations, publication of the Apple Users' Group, Sydney, Australia

THIS PAGE COPYRIGHT © 2007, ANDREW ROUGHAN